import {uSelectDevReportDataByQueryVo} from '@/api'
import ChildrenPageUtils from '@/store/extends'
import {getStorage} from '@/utils/localstoreage'
import Taro from '@tarojs/taro'
import {observable, computed, action} from 'mobx'
import {createContext} from 'react'

class Warning extends ChildrenPageUtils {
  @observable warningList: Device.WarningLog[] = []
  @observable startTime: string = ''
  @observable endTime: string = ''

  @action.bound
  initPageData() {
    this.warningList = []
    this.startTime = ''
    this.endTime = ''
  }
  @action.bound
  changeDateRange(s: string, e: string) {
    console.log('warning modal changeDateRange', s, e)
    this.resetPagination()
    this.startTime = s + ' 00:00:00'
    this.endTime = e + ' 23:59:59'
    this.warningList = []
    this.getPageData()
  }

  @action.bound
  async getPageData() {
    const devId = this.pageQuery.devId
    if (devId === undefined) return
    const userId = getStorage('userId')
    if (!userId) return

    this.closeUpdate()
    Taro.showLoading()
    uSelectDevReportDataByQueryVo({
      devId,
      userId,
      page: this.pagination.page,
      size: this.pagination.size,
      startTime: this.startTime,
      endTime: this.endTime
    })
      .then(res => {
        if (this.pagination.page === 1 && !res.data.list.length) {
          Taro.showModal({
            content: '此时间段内无报警信息'
          })
        }
        this.warningList.push(...res.data.list)
        this.updatePagination({
          page: res.data.page,
          total: res.data.total,
          totalPage: res.data.totalPage
        })
        this.openUpdate()
        Taro.hideLoading()
      })
      .catch(err => {
        this.openUpdate()
        this.delPage()
        Taro.hideLoading()
      })
  }
}
export const WarningStore = createContext(new Warning())
